﻿@model ManagePostsViewModel

@{
    ViewBag.Title = Strings.Posts;    
}

@using (Html.BeginForm())
{ 
    <fieldset class="shaded">
        <div class="title">@Strings.Posts</div>
        <div class="links">
            @Html.ActionLink(Strings.Post_CreateTitle, "Create", "Post")
        </div>
        <br />
        <fieldset class="form shaded filters">
            <label>@Strings.Search:</label>
            @Html.TextBoxFor(x => x.Query)
            <label>@Strings.Post_Author:</label>
            @Html.DropDownList("AuthorId", Model.Authors, Strings.Select + "...")
            <input type="submit" value='@Strings.Search' />
            <input type="button" value='@Strings.Reset' id="resetButton" />
        </fieldset>
        @(Html.Grid(Model.Data).Columns(column =>
    {
        column.For(x => x.Title);
        column.For(x => x.Author.FullName);
        column.For(x => x.PublishDate).Format("{0:MM/dd/yy HH:mm}");
        column.For(x => x.Featured);
        column.For(x => x.Published);
        column.For(x => Html.ActionLink(Strings.Edit, "Edit", "Post", new { id = x.PostId }, null) +
                    String.Format(" | <a href='#' id='{0}' class='deleteLink'>{1}</a>", x.PostId, Strings.Delete)).Encode(false);

    }).Sort(Model.GridSortOptions))
        @Html.Pager(Model.PagingInfo)
    </fieldset>
}
@section head {
    <script type="text/javascript">
        $(document).ready(function () {
            $(".deleteLink").click(function () {
                deletePost($(this));
            });

            $("#resetButton").click(function () {
                $("#Query").val("");
                $("#AuthorId").val("");
            });
        });

        function getPost(link) {
            var tr = link.closest('tr');
            post = new Object();
            post.ID = link.attr("id");
            post.Title = $('td:first', tr).text();
            return post;
        }

        function deletePost(link) {
            var tr = link.closest('tr');
            var post = getPost(link);
            var message = '@Html.Raw(Strings.Post_ConfirmDeleteFormatString)'.replace('{0}', post.Title);
            if (confirm(message) == false)
                return;

            $.post("/Post/Delete/", { 'id': post.ID }, function (data) {
                if (data.Success == true) {
                    tr.remove();
                }
                else {
                    alert(data.Message);
                }
            });
        }
    </script>
}